package com.applancer.adapter;

import java.util.List;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.applancer.model.GeneralModel;
import com.applancer.model.MenuModel;
import com.applancer.nativecmsflip.R;

public class LeftMenuExpandedAdapter extends BaseExpandableListAdapter {

	protected List<GeneralModel> ldata;

	public LeftMenuExpandedAdapter(List<GeneralModel> data) {
		ldata = data;
	}

	@Override
	public int getGroupCount() {
		return ldata.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return ((MenuModel) ldata.get(groupPosition)).getChilds().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return ldata.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return ((MenuModel) ldata.get(groupPosition)).getChilds().get(
				childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {

		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {

		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		ViewHolder holder;

		if (convertView == null) {

			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.adapter_menu_left, null);

			holder = new ViewHolder();
			holder.icon = null;
			holder.title = (TextView) convertView
					.findViewById(R.id.leftmenuitemtitle);

			holder.indicator = (ImageView) convertView
					.findViewById(R.id.indicator);
			holder.signal_view = (View) convertView
					.findViewById(R.id.signal_view);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		MenuModel menuItem = (MenuModel) getGroup(groupPosition);

		if (menuItem.getChilds().size() > 0) {
			holder.indicator.setVisibility(View.VISIBLE);
			holder.signal_view.setVisibility(View.VISIBLE);

			if (isExpanded) {
				holder.indicator
						.setImageResource(R.drawable.ic_leftmenu_collapse);
			} else {
				holder.indicator
						.setImageResource(R.drawable.ic_leftmenu_expanded);
			}

		} else {
			holder.signal_view.setVisibility(View.INVISIBLE);
			holder.indicator.setVisibility(View.GONE);

		}

		holder.title.setText(menuItem.getTitle());
		holder.title.setTextColor(Color.parseColor("#006699"));

		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {

			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.adapter_menu_left, null);

			holder = new ViewHolder();
			holder.icon = null;
			holder.title = (TextView) convertView
					.findViewById(R.id.leftmenuitemtitle);
			holder.indicator = (ImageView) convertView
					.findViewById(R.id.indicator);
			holder.signal_view = (View) convertView
					.findViewById(R.id.signal_view);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.signal_view.setVisibility(View.INVISIBLE);

		MenuModel menuItem = (MenuModel) getChild(groupPosition, childPosition);
		holder.title.setText(menuItem.getTitle());
		holder.title.setTextColor(Color.rgb(50, 50, 50));
		holder.title.setPadding(35, 0, 0, 0);
		holder.indicator.setVisibility(View.GONE);

		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public static class ViewHolder {
		ImageView icon;
		TextView title;
		ImageView indicator;
		View signal_view;
	}

}
